package top.yueyazhui.ask_for_leave.service;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import top.yueyazhui.ask_for_leave.entity.Role;
import top.yueyazhui.ask_for_leave.mapper.UserMapper;
import top.yueyazhui.ask_for_leave.entity.User;

import java.util.List;

@Service
public class UserService implements UserDetailsService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.loadUserByUsername(username);
        if(ObjectUtil.isNull(user)) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        user.setRoleList(userMapper.getRoleListByUserId(user.getId()));
        return user;
    }

    public List<User> getApproveUserList(Integer currentUserId) {
        return userMapper.getApproveUserList(currentUserId);
    }

    public String getUsernameById(Integer applicantId) {
        return userMapper.getUsernameById(applicantId);
    }
}
